package com.md.infrastructure.config;

import com.md.util.CollectionUtil;
import com.md.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.opensaml.saml.common.xml.SAMLConstants;
import org.opensaml.saml.saml2.metadata.EntityDescriptor;
import org.opensaml.saml.saml2.metadata.SingleLogoutService;
import org.opensaml.saml.saml2.metadata.SingleSignOnService;

import java.security.cert.X509Certificate;
import java.util.List;

/**
 * @author 刘骄阳
 * 2024-03-23 2:32
 */
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
public class AzureSamlConfig {

  private String spUrl;
  /**
   * 跳转微软的登录地址
   */
  private String loginUrl;

  /**
   * 跳转微软的登出地址
   */
  private String logoutUrl;

  /**
   * 登录成功微软回调地址
   */
  private String acsUrl;

  /**
   * 应用id
   */
  private String entityId;

  /**
   * 需要验签(true: 需要; false: 不需要)
   */
  private Boolean needSigned;

  /**
   * 解密用的证书
   */
  private List<X509Certificate> verificationCertificates;
  /**
   * 加密用的证书
   */
  private List<X509Certificate> encryptionCertificates;
  /**
   * 记录SAML通信详细信息
   */
  private EntityDescriptor descriptor;

  /**
   * 数字签名的算法
   */
  private List<String> signingAlgorithms;

  /**
   * 登录
   */
  private List<JumpEntity> signOnServiceBindingList;
  /**
   * 登出
   */
  private List<JumpEntity> signOutServiceBindingList;

  /**
   * 元数据url
   */
  private String mediaXMLUrl;

  /**
   * 设置登出配置
   *
   * @param singleSignOnServices 登出绑定的参数
   */
  public void setSignOutServiceBinding(final List<SingleLogoutService> singleSignOnServices) {
    this.signOutServiceBindingList = CollectionUtil.mapFilter(singleSignOnServices,
      singleSignOnService -> {
        JumpEntity jumpEntity = JumpEntity.builder().method(singleSignOnService.getBinding())
          .url(singleSignOnService.getLocation()).build();
        if (ObjectUtil.isEmpty(singleSignOnService.getResponseLocation())) {
          jumpEntity.setResponseUrl(singleSignOnService.getLocation());
        } else {
          jumpEntity.setResponseUrl(singleSignOnService.getResponseLocation());
        }
        return jumpEntity;
      }, singleSignOnService -> ObjectUtil.hasEquals(singleSignOnService.getBinding(),
        SAMLConstants.SAML2_POST_BINDING_URI, SAMLConstants.SAML2_REDIRECT_BINDING_URI));
  }

  public JumpEntity getSignOnServiceBinding() {
    return this.signOnServiceBindingList.get(signOnServiceBindingList.size() - 1);
  }

  /**
   * 设置登录配置
   *
   * @param singleSignOnServiceList 登录绑定的参数
   */
  public void setSignOnServiceBinding(final List<SingleSignOnService> singleSignOnServiceList) {
    this.signOnServiceBindingList = CollectionUtil.mapFilter(singleSignOnServiceList, singleSignOnService ->
      JumpEntity.builder().method(singleSignOnService.getBinding()).url(singleSignOnService.getLocation())
        .responseUrl(singleSignOnService.getResponseLocation()).build(), singleSignOnService ->
      ObjectUtil.hasEquals(singleSignOnService.getBinding(), SAMLConstants.SAML2_POST_BINDING_URI,
        SAMLConstants.SAML2_REDIRECT_BINDING_URI));
  }
}

